package com.openlogic.common.security.utils;

import java.util.List;

import org.noear.nami.Nami;
import org.noear.solon.Solon;
import org.noear.solon.core.handle.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openlogic.common.core.exception.UtilException;
import com.openlogic.common.core.web.domain.entity.SysUser;
import com.openlogic.common.security.entity.SysTenantEntity;
import com.openlogic.common.security.rpc.RemoteSecuritySysUserService;
import com.openlogic.common.security.rpc.RemoteSecurityTenantService;

import cn.dev33.satoken.stp.StpUtil;

/**
 * @ClassName: OpenLogicUtils
 * @Description:  框架通用工具类，可远程获取系统数据
 * @author weiyongpeng
 * @date 2025年1月30日 下午8:55:58
 */
public class OpenLogicUtils {
	
	private static final Logger log = LoggerFactory.getLogger(OpenLogicUtils.class);

	private static RemoteSecurityTenantService remoteTenantService = Solon.context()
			.getBean(RemoteSecurityTenantService.class);
	private static RemoteSecuritySysUserService remoteSysUserService = Solon.context()
			.getBean(RemoteSecuritySysUserService.class);

	static {
		if (remoteTenantService == null) {
			remoteTenantService = Nami.builder().create(RemoteSecurityTenantService.class);
			Solon.context().beanMake(RemoteSecurityTenantService.class);
		}
		if (remoteSysUserService == null) {
			remoteSysUserService = Nami.builder().create(RemoteSecuritySysUserService.class);
			Solon.context().beanMake(RemoteSecuritySysUserService.class);
		}
	}

	/**
	 * 通过租户ID获取租户信息
	 * 
	 * @return
	 */
	public static SysTenantEntity getTenantInfo() {
		Result<SysTenantEntity> result = remoteTenantService.getTenantInfo();
		if (result.getCode() == Result.SUCCEED_CODE) {
			return result.getData();
		} else {
			log.error("租户不存在：{}", result.getDescription());
			throw new UtilException("租户不存在！");
		}
	}

	/**
	* 描述：通过登录id获取用户信息
	* @Title: current
	* @return
	* @author weiyongpeng
	* @date  2025年1月30日 下午9:39:39
	 */
	public static SysUser current() {
		Result<SysUser> result = remoteSysUserService.getUserById(StpUtil.getLoginId().toString());
		if (result.getCode() == Result.SUCCEED_CODE) {
			return result.getData();
		} else {
			log.error("租户不存在：{}", result.getDescription());
			throw new UtilException("租户不存在！");
		}
	}

	/**
	* 描述：当前登录用户权限列表
	* @Title: permissionList
	* @return
	* @author weiyongpeng
	* @date  2025年1月30日 下午9:40:23
	 */
	public static List<String> permissionList() {
		Result<List<String>> result = remoteSysUserService.getUserPermissionList(StpUtil.getLoginIdAsString());
		if (result.getCode() == Result.SUCCEED_CODE) {
			return result.getData();
		} else {
			log.error("用户不存在！", result.getDescription());
			throw new UtilException("用户不存在！");
		}
	}

	/**
	* 描述：当前登录用户角色列表 
	* @Title: roleList
	* @return
	* @author weiyongpeng
	* @date  2025年1月30日 下午9:41:11
	 */
	public static List<String> roleList() {
		Result<List<String>> result = remoteSysUserService.getUserRoleList(StpUtil.getLoginIdAsString());
		if (result.getCode() == Result.SUCCEED_CODE) {
			return result.getData();
		} else {
			log.error("用户不存在！", result.getDescription());
			throw new UtilException("用户不存在！");
		}
	}

}
